home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
Frameworks
/
Extension Shell 1.5
/
Sample Extensions (1.5)
/
Bell Test ƒ
/
ES Handler.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-04-12
|
8KB
|
338 lines
/* NAME:
ES Handler.c
WRITTEN BY:
Dair Grant
DESCRIPTION:
This file contains an ES Handler for use by Extension Shell.
___________________________________________________________________________
*/
//=============================================================================
// Include files
//-----------------------------------------------------------------------------
#include <Resources.h>
#include <Gestalt.h>
#include <Traps.h>
#include "A4Stuff.h"
#include "SetupA4.h"
#include "ES.h"
#include "BT AddrsTable.h"
#include "BT Constants.h"
#include "ES Handler.h"
//=============================================================================
// Private function prototypes
//-----------------------------------------------------------------------------
void main(short theMsg, ESParamBlock *theParamBlock);
void InitialiseParamBlock(void);
void InitialiseAddrsTable(void);
void HandleTheError(void);
void SetUpIcons(short animDelay, short numIcons, short firstIcon);
//=============================================================================
// Global variables
//-----------------------------------------------------------------------------
ESParamBlock *gTheParamBlock;
//=============================================================================
// main : Entry point to our code resource.
//-----------------------------------------------------------------------------
// Note : Extension Shell communicates with us via a message constant,
// and a pointer to a structure it owns. Our job is to fill in
// the details in that structure, depending on what it wants us
// to do.
//-----------------------------------------------------------------------------
void main(short theMsg, ESParamBlock *theParamBlock)
{ long oldA4;
// Set up A4 so that we can access our globals
#ifndef powerc
oldA4 = SetCurrentA4();
#endif
gTheParamBlock = theParamBlock;
// Case out on what we have to do
switch(theMsg) {
case kInitialiseParamBlock:
InitialiseParamBlock();
break;
case kInitialiseAddrsTable:
InitialiseAddrsTable();
break;
case kHandleError:
HandleTheError();
break;
default:
;
}
// Restore A4
#ifndef powerc
SetA4(oldA4);
#endif
}
//=============================================================================
// InitialiseParamBlock : Initialises the ParamBlock.
//-----------------------------------------------------------------------------
// Note : We have three tasks to perform.
// • Check to see if we can run
// • Set up the icons we want to display
// • Set up the code we want installed
//-----------------------------------------------------------------------------
void InitialiseParamBlock(void)
{
// Check for System 7. We depend on having System 7, and won't
// run if we don't have it. If we don't have it, we beep, post
// an error message, and show our disabled icon(s).
if (gTheParamBlock->systemVersion < 0x0700)
{
// Error details
gTheParamBlock->beepNow = true;
gTheParamBlock->postError = true;
gTheParamBlock->errorStringsID = kErrorStrings;
gTheParamBlock->errorStringIndex = kNeedSystemSeven;
// Icon details
SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
}
// If a shift key, or the mouse button, is down, we don't load either.
// We don't post an error, but we do show our disabled icon(s) to let
// the user know they've turned us off.
else if ((*gTheParamBlock->IsKeyMouseDown)(kShiftKey, true))
{
// Icon details
SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
}
// Otherwise, we're allowed to run. If we had a Control Panel, we
// would also check to see if our Control Panel had turned us
// off (by setting some preference resource).
else
{
// Icon details
SetUpIcons(kEnabledAnimDelay, kMyNumEnabledIcons, kMyFirstEnabledIcon);
// We install one trap patch, one code block, and request an address table
gTheParamBlock->installAddressTable = true;
gTheParamBlock->addressTableSelector = kBellTestAddressTable;
gTheParamBlock->numCodeResources = 2;
// Details for a trap patch to MenuSelect()
gTheParamBlock->theCodeResources[kMenuSelect].resType = kMenuSelectResType;
gTheParamBlock->theCodeResources[kMenuSelect].resID = kMenuSelectResID;
gTheParamBlock->theCodeResources[kMenuSelect].codeType = kTrapPatchType;
gTheParamBlock->theCodeResources[kMenuSelect].theCodeThing.theTrapPatch.trapWord = _MenuSelect;
gTheParamBlock->theCodeResources[kMenuSelect].theCodeThing.theTrapPatch.globalpatch = true;
// Details for a code block
gTheParamBlock->theCodeResources[kPlaySound].resType = kPlaySoundResType;
gTheParamBlock->theCodeResources[kPlaySound].resID = kPlaySoundResID;
gTheParamBlock->theCodeResources[kPlaySound].codeType = kCodeBlockType;
gTheParamBlock->theCodeResources[kPlaySound].theCodeThing.theCodeBlock.refCon = 0x00000000;
}
}
//=============================================================================
// InitialiseAddrsTable : Initialise the address table.
//-----------------------------------------------------------------------------
// Note : If we requested an address table, Extension Shell calls us back
// to allow us to initialise any extensions we've made to it.
//
// This routine will only be called if we request an address
// table, and is called after the address table is installed,
// but before any of the items in gTheParamBlock->theCodeResources
// are processed (since they might need to access the address
// table).
//
// We should initialise the magicNumber and versionNumber fields
// with constants fixed for this build. One possible number for the
// magicNumber field would be sizeof() our address table structure.
//-----------------------------------------------------------------------------
void InitialiseAddrsTable(void)
{ BTAddressTable *theAddressTable;
Handle theHnd;
// Call Gestalt to find our address table
Gestalt(kBellTestAddressTable, (long *) &theAddressTable);
// Initialise the magic number and version fields
theAddressTable->magicNumber = kMyTableMagic;
theAddressTable->versionNumber = kMyTableVersion;
// Initialise our custom values
theHnd = GetResource('snd ', kSoundToPlay);
DetachResource(theHnd);
theAddressTable->theSound = theHnd;
}
//=============================================================================
// HandleTheError : Handle any errors
//-----------------------------------------------------------------------------
// Note : This routine is called if an error occurred during the
// installation of the items in gTheParamBlock->theCodeResources.
//
// If an error occurs we beep, post an error, and request that
// as much as possible of our code be uninstalled. We also reset
// the icon details to show our disabled icons.
//-----------------------------------------------------------------------------
void HandleTheError(void)
{
// Decide how we want to handle the error
gTheParamBlock->removeInstalledCode = true;
gTheParamBlock->beepNow = true;
gTheParamBlock->postError = true;
gTheParamBlock->errorStringsID = kErrorStrings;
// Message to display to the user
gTheParamBlock->errorStringIndex = kUnknownError;
// Icon details
SetUpIcons(kDisabledAnimDelay, kMyNumDisabledIcons, kMyFirstDisabledIcon);
}
//=============================================================================
// SetUpIcons : Set up our the icon fields in gTheParamBlock.
//-----------------------------------------------------------------------------
// Note : We are passed in the resource ID of the first icon, the number
// of icons, and a delay for animation. We fill these details
// in to gTheParamBlock.
//-----------------------------------------------------------------------------
void SetUpIcons(short animDelay, short numIcons, short firstIcon)
{ short i;
gTheParamBlock->animationDelay = animDelay;
gTheParamBlock->numIcons = numIcons;
for (i = 1; i <= numIcons; i++)
gTheParamBlock->theIcons[i] = firstIcon + i - 1;
}